﻿#include <stdio.h>
#include <string.h>
#include "dlt645_api_07.h"
#include "dlt645_port.h"

int g_num = 0;

void show_packet(int buflen, char* buf);
int show_ruler_info(uint32_t inRulerID);

extern int g_need_pack_07;


void debug_switch(int show)
{
	 g_need_pack_07 = show;
}

void unpack_d07_frame_read_data(uint8_t *buf, int len, S_D07_UNPACK *stUnPack)
{

	int i,j = 0;
	int ret = 0;

	debug_switch(D07_ON);
	ret = unpack_d07_frame(buf, len, stUnPack);
	debug_switch(D07_OFF);
	if(ret != E_D07_OK)
	{
		printf("\n  解析出错! (error = %d : ",ret);
		switch(ret)
		{
			case E_D07_ERRO_FRAME_UNCOMP:
				printf("不完整的帧数据)\n");
				break;

			case E_D07_ERRO_FRAME_0x68:
				printf("起始符 0x68 的位置不对)\n");
				break;
				
			case E_D07_ERRO_FRAME_CHECK_SUM:
				printf("测试帧检验和不对)\n");
				break;
				
			case E_D07_ERRO_FRAME_END_0x16:
				printf("测试帧结束符 0x16 不对)\n");
				break;
			default:
				break;
		}
		
		printf("\n");
		return;
	}
	
	// 显示结果
	printf("/------------------unpack result-------------------\\\n");
	printf("\n<1> - ruler info:\n");
	show_ruler_info(stUnPack->ruler_id);
	printf("\n<2> - frame info:\n");
	printf(" ctrl_code\t=  0x%02X\n", stUnPack->ctrl_c);
	printf(" 0xFE_num\t=  %d\n", stUnPack->lead_num);
	printf(" data_len\t=  %d\n", stUnPack->data_len);
	printf(" frame_len\t=  %d\n", stUnPack->frame_len);
	//stUnPack->address[12] = 0;
	printf(" address\t=  %s\n", stUnPack->address);
	printf("\n<3> - data fields \n");
	if(stUnPack->flag == E_D07_UNPD_FLG_OK)
	{
		printf("正确的回答:\n");
	}
	else if(stUnPack->flag == E_D07_UNPD_FLG_ERROR_OK)
	{
		printf("异常的回答:\n");
	}
	printf("%s\n", g_out_data_07);

	printf("\n<4> - remark:\n\n\t");
	if(stUnPack->ctrl_s.direct == E_D07_CTRL_DIR_M2S)
	{
		printf("主站到从站的");
		if(E_D07_CTRL_READ_DATA == stUnPack->ctrl_s.funcode)
		{
			printf("读数据");
		}
		else if(E_D07_CTRL_WRITE_DATA == stUnPack->ctrl_s.funcode)
		{
			printf("写数据");
		}
		printf("请求\n\n");
	}
	else
	{		
		printf("从站到主站的");
		if(E_D07_CTRL_READ_DATA == stUnPack->ctrl_s.funcode)
		{
			printf("对读数据请求的响应");
		}
		else if(E_D07_CTRL_WRITE_DATA == stUnPack->ctrl_s.funcode)
		{
			printf("对写数据成功的响应");
		}
		printf("\n\n");
	}

	printf("\\------------------unpack result-------------------/\n");
}




void pack_d07_frame_read_data(char * addr, uint32_t inRulerID, S_D07_UNPACK *stUnPack)
{
	int num = 1;

	printf("0x%02X\n", inRulerID);
	S_D07_RULER_INFO info = {0};
	int ret = get_d07_ruler_info(inRulerID, &info);
	if(ret != E_D07_OK)	
	{
		printf("不存在的规规约类型\n\n");
		return ;
	}

	unsigned char ucCtrl = 0;
	S_D07_CTRL_CODE stCtrl = {0};
	int dir = 0;
	
	//char addr[64] = "000000000001"; // 地址
	S_D07_PACK_FRAME pack_frame = {0};
	int length = 0;
	char buffer[256] = {0};
	char data[256] = {0};
	char user[256] = {0};
	F_D07_RULER_TRANS func = NULL;
	
	dir = 0; // 传输方向:	主站到从站(0)	从站到主站(1)
	stCtrl.direct = (E_D07_CTRL_DIR)dir;

	// 主站到从站
	if(E_D07_CTRL_DIR_M2S == dir)
	{
		if(info.rdwr == E_D07_RDWR_READ_ONLY)
		{
			stCtrl.funcode = E_D07_CTRL_READ_DATA;
			pack_frame.data_len = 4;
			pack_frame.data = NULL;
		}
		else if(info.rdwr == E_D07_RDWR_READ_WRITE)
		{
			//功能码
			printf("\n%d - 功能码:  读数据(0) 写数据(1)\n operate << ",num++);
			dir = 0;
			if(dir == 0)
			{
				stCtrl.funcode = E_D07_CTRL_READ_DATA;
				pack_frame.data_len = 4;
				pack_frame.data = NULL;
			}
			else if(1 == dir)
			{
				debug_switch(D07_ON);
				stCtrl.funcode = E_D07_CTRL_WRITE_DATA;
				pack_frame.data_len = info.len + 4;
				func = info.func;
				func(E_D07_TRANS_U2F,user, data);
				pack_frame.data = data;
				debug_switch(D07_OFF);
			}
			else
			{
				printf("\n输入错误\n");
				return;
			}
		}
	}
	else	// 从站到主站
	{
		printf("\n%d - 数据域内容:\n\n",num++);
		// 正确的应答
		func = info.func;
		debug_switch(D07_ON);
		func(E_D07_TRANS_U2F, user, data);
		debug_switch(D07_OFF);
		stCtrl.funcode = E_D07_CTRL_READ_DATA;
		pack_frame.data = data;
		pack_frame.data_len = info.len + 4;
		// 异常的应答
	}

	ret = trans_d07ctrl_struct2char(&ucCtrl, &stCtrl); //将结构封装成字节
	
	if(ret != E_D07_OK)
	{
		printf("\ntrans_d07ctrl_struct2char failed %d\n", ret);
		return;
	}
	
	pack_frame.ruler_id = inRulerID;
	pack_frame.ctrl_code = ucCtrl;
    memcpy(pack_frame.address,addr, 13 );

	ret = pack_d07_frame_by_data(&pack_frame, buffer, &length);
	if(ret != E_D07_OK)
	{
		printf("\npack frame failed %d\n", ret);
		return;
	}

	printf("\n%d - ruler infor:\n\n", num++);
	show_ruler_info(inRulerID);
	printf(" ctrl\t=  0x%02X\n", ucCtrl);
	printf(" addr\t=  %s\n", addr);

	
	//显示帧内容
	printf("\n%d - frame:\n\n\t", num++);
	show_packet(length, buffer);
	printf("\n\n-------------------------------------------------------------------\n");
	
	dlt645.write(NULL, (uint8_t *)buffer, length);
	static uint8_t read_buf[64];
	int read_len=0;
	read_len = dlt645.read(NULL,read_buf, sizeof(read_buf));
	if(read_len > 0){
		printf("\n%d - recv %d byte:\n\n\t", num++, read_len);
		show_packet(read_len, (char *)read_buf);
		unpack_d07_frame_read_data(read_buf, read_len, stUnPack);
	}

}

static void show_packet(int buflen, char* buf)
{	
    
	int j = buflen;
	int i;
	//printf("packet length :%d\n packet content : ",j);
	for(i = 0; i < j;i++)
	{
		printf("%02X ", (unsigned char)buf[i]);
	} 
}

